/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Yahoo;

import API.Item;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class TextDisplay extends Item {

    private Vector datas;
    private int count;
    private Vector display;
    private int index;

    public TextDisplay(int x, int y, int width, int height) {
        this.datas = new Vector();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        count = height / font.getHeight();
        index = this.datas.size() - 1;
        Init();
    }

    public Vector getDatas() {
        return datas;
    }

    public void setDatas(Vector datas) {
        this.datas = datas;
        index = datas.size() - 1;
        Init();
    }

    public void KeyPress(int gameCode) {
        switch (gameCode) {
            case Canvas.DOWN:
                if (index < datas.size() - 1) {
                    index++;
                    Init();
                }
                break;
            case Canvas.UP:
                if (index > count) {
                    index--;
                    Init();
                }
                break;
        }
    }

    public void AddMessage(String message) {
        Vector v = GetAllString(message);
        for (int i = 0; i < v.size(); i++) {
            datas.addElement(v.elementAt(i));
        }
        index = datas.size() - 1;
        Init();
    }

    private void Init() {
        if (this.datas.size() <= count) {
            display = new Vector();
            for (int i = datas.size() - 1; i >= 0; i--) {
                display.addElement(datas.elementAt(i));
            }
        } else {
            if (datas.size() - index < count) {
                display = new Vector();
                for (int i = index; i > index - count; i--) {
                    display.addElement(datas.elementAt(i));
                }
            }
        }
    }

    private Vector GetAllString(String message) {
        String first = message.substring(0, 1);
        message = message.substring(1);
        Vector v = new Vector();
        int i = message.length();
        String str;
        while (true) {
            if (font.stringWidth(message) <= width - 2) {
                break;
            }
            i--;
            str = message.substring(0, i);
            if (font.stringWidth(str) <= width) {
                v.addElement(first + str);
                message = message.substring(i + 1);
                i = message.length();
            }
        }
        v.addElement(first + message);
        return v;
    }

    public void Paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(x, y, width, height);
        g.setFont(font);
        int tmpy = y + height;
        for (int i = 0; i < display.size(); i++) {
            String str = display.elementAt(i).toString();
            if (str.startsWith("+")) {
                g.setColor(0);
                g.drawString(str.substring(1), x + 1, tmpy, 0);
            } else {
                g.setColor(0x0000ff);
                g.drawString(str.substring(1), x + width - 1, tmpy, Graphics.RIGHT | Graphics.TOP);
            }
            tmpy -= font.getHeight();
        }
    }
}
